package com.teng.smartdrawer;

import android.app.Dialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.teng.smartdrawer.adapter.GridAdapter;
import com.teng.smartdrawer.adapter.MenuAdapter;
import com.teng.smartdrawer.adapter.RecommendAdapter;
import com.teng.smartdrawer.bean.Module;
import com.teng.smartdrawer.bean.Part;
import com.teng.smartdrawer.bean.PopWindowMenu;
import com.teng.smartdrawer.bean.Recommend;
import com.teng.smartdrawer.listener.DeleteListener;
import com.teng.smartdrawer.parser.DataConstants;
import com.teng.smartdrawer.parser.DataParser;
import com.teng.smartdrawer.popUtils.ScreenUtils;
import com.teng.smartdrawer.view.HeaderGridView;

import java.util.ArrayList;
import java.util.List;


public class SearchModuleActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener, AdapterView.OnItemLongClickListener, DeleteListener {

    private List<Recommend> mData = new ArrayList<Recommend>();
    private GridAdapter adapter;
    private SwipeRefreshLayout refresh_layout;
    private Dialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_module);

        ActionBar actionBar = getSupportActionBar();

        // 显示返回图表
        actionBar.setDisplayHomeAsUpEnabled(true);

        //设置ActionBar左边默认的图标是否可用
        actionBar.setDisplayUseLogoEnabled(true);

        // 显示窗口标题
        actionBar.setDisplayShowTitleEnabled(true);

        actionBar.setTitle("模块");

        //actionBar.setSubtitle("模块");

        refresh_layout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        refresh_layout.setColorSchemeResources(R.color.google_blue, R.color.google_yellow, R.color.google_blue, R.color.google_yellow);
        refresh_layout.setDistanceToTriggerSync(500);   // 下拉刷新的距离
        refresh_layout.setOnRefreshListener(this);

        refresh_layout.post(new Runnable() {
            @Override
            public void run() {
                refresh_layout.setRefreshing(true);
            }
        });

        GridView gridView = (GridView) findViewById(R.id.module_header_grid_view);
        gridView.setNumColumns(3);

        adapter = new GridAdapter(this , mData);
        adapter.setDeleteListener(this);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(this);
        gridView.setOnItemLongClickListener(this);

//        initDialog();

        DataParser.getModuleList(this, handler);
    }


    private void initDialog(){
        loadingDialog = new Dialog(this , R.style.defind_dialog);

        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(R.layout.subscribe_menu_popwindow, null);
        ListView mMenuListView = (ListView) v.findViewById(R.id.menu_listview);

        LinearLayout menu_linearlayout = (LinearLayout) v.findViewById(R.id.menu_linearlayout);
        menu_linearlayout.setOnClickListener(this);

        ArrayList<PopWindowMenu> list = new ArrayList<PopWindowMenu>();
        list.add(new PopWindowMenu(0, 0, "添加模块"));

        MenuAdapter mMenuA = new MenuAdapter(this, list);
        mMenuListView.setAdapter(mMenuA);

        Window window = loadingDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);

        loadingDialog.setCancelable(true);// 不可以用“返回键”取消
        loadingDialog.setContentView(v);// 设置布局

        WindowManager.LayoutParams lp = loadingDialog.getWindow().getAttributes();
        lp.width = (int) (ScreenUtils.getScreenWidth(this)); // 设置宽度
        loadingDialog.getWindow().setAttributes(lp);

        mMenuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(SearchModuleActivity.this, "添加模块", Toast.LENGTH_LONG).show();

            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu_component , menu);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO actionBar返回处理
        if(item.getItemId() == android.R.id.home)
        {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);

    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {

            switch (msg.what){
                case DataConstants.GET_MODULE_SIGN:

                    List<Recommend>  list = (List<Recommend>) msg.obj;

                    mData.clear();
                    mData.addAll(list);
                    adapter.notifyDataSetChanged();

                    refresh_layout.setRefreshing(false);

                    break;
                default:
                    break;
            }

            return false;
        }
    });


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.actionbar_back:
                finish();
                break;
            case R.id.menu_linearlayout:
                loadingDialog.dismiss();
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        intent.setClass(this,PartActivity.class);
        Module module = (Module) mData.get(position);
        intent.putExtra("id" , module.getD_id());
        intent.putExtra("name" , module.getD_name());
        intent.putExtra("type" , 1);
        startActivity(intent);
    }


//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        int isShowDelete = adapter.getIsShowDelete();
//        if (isShowDelete == 1){
//            adapter.setIsShowDelete(0);
//            adapter.notifyDataSetChanged();
//            return true;
//        }
//        Log.e("setIsShowDelete" , adapter.getIsShowDelete()+" ----- ");
//        return super.onTouchEvent(event);
//    }

    public void component_menu(MenuItem item) {
        loadingDialog.show();
    }

    @Override
    public void onRefresh() {
        DataParser.getModuleList(this, handler);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        adapter.setIsShowDelete(1);
        adapter.notifyDataSetChanged();
        return false;
    }

    @Override
    public void deleteRecommend(Recommend recommend) {

    }
}
